
import { showModal, showToast, requestPayment } from "../../utils/asyncWx";
import { request } from "../../request/index";
import {setOrderInfo,getOrderInfo} from "../../api/order.js";
import {deleteCartOne} from "../../api/cart.js";




Page({
    data: {
        chooseAddress: {},
        cart: [],
        totalPrice: 0,
        totalNum: 0,
        order:[]
    },

    onShow:async function() {
        const userid = wx.getStorageSync('userid')
        let getorder = await getOrderInfo(userid)
        console.log(getorder);
        let cart = await request({
            url: "/cart/"+userid,
            method:"get",
        });
        this.handleChooseAddress()
        cart = cart.filter(v => v.is_check);
        let totalPrice = 0;
        let totalNum = 0;
        cart.forEach(v => {
            totalPrice += v.number * v.goods_price;
            totalNum += v.number;
        })
        this.setData({
            cart,
            totalPrice,
            totalNum,
            order:getorder
        })
    },

    async handleChooseAddress() {
        let userid= wx.getStorageSync('userid')
        const address = await request({
            url: "/address/"+userid+"/",
            method:"get",
            data:{
                userid
            }
      });
     var chooseAddress = address.find(v=>v.isChecked==true)
      this.setData({
        chooseAddress
      })
    },

    async handleOrderPay() {
        var userid = wx.getStorageSync('userid')
        var createOrder= wx.getStorageSync('createOrder')
        var cart = this.data.cart
        var order = this.data.order
        createOrder.forEach(async v1=>{
            const order = await setOrderInfo(v1.id,"1")  
            })  
        cart.forEach(async v1=>{
        await deleteCartOne(userid,v1.id)
    }) 
        wx.navigateTo({
            url: '/pages/order/index?type=2',
        });
    },

})